/******************************************************************
 * @Title: MailSender.java 
 * @Package com.omni.web.utils 
 * @Description: TODO
 * @author Dellinger
 * @date 2015年4月25日 下午4:43:51 
 * @version v1.0 
 * @copyright Omnikennect.com
 ******************************************************************/
package com.omni.web.utils;

import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/** 
 * @ClassName: MailSender 
 * @Description: TODO 
 * @author Dellinger
 * @date 2015年4月25日 下午4:43:51 
 *  
 */
public class MailSender {

	private final static Logger logger = LoggerFactory.getLogger(MailSender.class);
	
	private final BeanFactory factory;
	/** 
	 * <p>Title: </p> 
	 * <p>Description: </p>  
	 */
	public MailSender(BeanFactory factory) {
		
		this.factory = factory;
	}
	
	public boolean sendHtml(Map<String, String> map) {
		
		logger.debug("MailSender::sendHtml");
		boolean result = false;
		JavaMailSenderImpl sender = (JavaMailSenderImpl) factory.getBean("mailSender");
		
		try {
			if (sender != null) {
				MimeMessage message = sender.createMimeMessage();
				MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "utf-8");
				messageHelper.setTo(map.get("to"));
				messageHelper.setFrom(map.get("from"));
				messageHelper.setSubject(map.get("subject"));
				messageHelper.setText(map.get("content"), true);
				
				sender.send(message);
				result = true;
			}
		}
		catch (Exception ex) {
			logger.error("MailSender::sendHtml..FAIL!", ex);
		}
		return result;
	}
}
